﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace VigenereCipher.Models
{
    /// <summary>
    /// 解密类
    /// </summary>
    internal class MyDecode
    {
        /// <summary>
        /// 明文
        /// </summary>
        public string PlainText { get; set; }
        /// <summary>
        /// 密文
        /// </summary>
        public string CipherText { get; set; }
        /// <summary>
        /// 密钥
        /// </summary>
        public string Key { get; set; }
        public void Decode()
        {
            //解密过程
            char[] plaintext = new char[CipherText.Length];
            //判断输入的密文的大小写
            for (int i = 0; i < CipherText.Length; i++)
            {
                int k1 = 0, k2 = 0;
                if (CipherText[i] >= 'a' && CipherText[i] <= 'z')
                    k1 = 97;
                else if (CipherText[i] >= 'A' && CipherText[i] <= 'Z')
                    k1 = 65;
                if (Key[i % Key.Length] >= 'a' && Key[i % Key.Length] <= 'z')
                    k2 = 97;
                else if (Key[i % Key.Length] >= 'A' && Key[i % Key.Length] <= 'Z')
                    k2 = 65;
                plaintext[i] = Convert.ToChar((CipherText[i] - k1 - Key[i % Key.Length] + k2 + 26) % 26 + k1);
            }
            PlainText = new string(plaintext);
        }
    }
}
